package com.qf.commons.standar.vaild;

import com.qf.commons.core.utils.ApplicationUtils;
import org.springframework.stereotype.Component;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Component
public class QfConstraint implements ConstraintValidator<QfVaild, Object> {

    private QfVaild qfVaild;

    @Override
    public void initialize(QfVaild qfVaild) {
        this.qfVaild = qfVaild;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        if (value==null) return true;
        Class<? extends IQfVaild> handler= qfVaild.handler();

        IQfVaild iQfVaild = ApplicationUtils.getBean(handler);
        if (iQfVaild ==null) return true;

        return iQfVaild.vaild(qfVaild,value);
    }
}
